﻿using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.ComponentModel;
using System.Reflection;

namespace TK.Common.Swagger
{
    /// <summary>
    /// swagger 过滤器
    /// </summary>
    public class EnumDocumentFilter : IDocumentFilter
    {
        /// <summary>
        /// apply
        /// </summary>
        public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
        {
            var schemaDic = context.SchemaRepository.Schemas;
            foreach (var item in schemaDic)
            {
                if (EnumDescriptionList.Dic.ContainsKey(item.Key))
                {
                    item.Value.Description = EnumDescriptionList.Dic[item.Key];
                }
                else
                {
                    foreach (var pro in item.Value.Properties)
                    {
                        if (pro.Value.Enum.Count > 0)
                        {
                            if (EnumDescriptionList.Dic.ContainsKey(pro.Value.Type))
                            {
                                pro.Value.Description = EnumDescriptionList.Dic[pro.Value.Type];
                            }
                        }
                    }
                }
            }

            EnumDescriptionList.Dic.Clear();
        }
    }
}
